import 'dart:math';
import 'dart:ui';

import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/my/conteroller/my_invite_conteroller.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/my/page/invite/my_invite_record_page.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/status_view.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:share/share.dart';

class MyInvitePage extends StatefulWidget {
  const MyInvitePage({Key? key}) : super(key: key);

  @override
  _MyInvitePageState createState() => _MyInvitePageState();
}

class _MyInvitePageState extends State<MyInvitePage> {
  ///状态
  late int status = LoadStatusType.loading;

  late InviteFriendsModel model;

  GlobalKey _globalKey = GlobalKey();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  Future<void> getData() async {
    try {
      model = await ServiceRepository.inviteFriends();
      status = LoadStatusType.success;
    } catch (e, s) {
      status = LoadStatusType.empty;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            width: double.infinity,
            height: double.infinity,
            child: LoadAssetImage(
              "invite_background",
              fit: BoxFit.cover,
            ),
          ),
          navWidget(),
          shareCode(),
        ],
      ),
    );
  }

  Widget navWidget() {
    return Container(
      height: Get.statusBarHeight,
      child: Padding(
        padding: const EdgeInsets.only(bottom: 0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            IconButton(
              padding: const EdgeInsets.only(right: 12, top: 12, bottom: 12),
              icon: Image.asset(
                'assets/images/ic_back_white.png',
              ),
              onPressed: () {
                Get.back();
              },
            ),
            IconButton(
              padding: const EdgeInsets.only(right: 12, top: 12, bottom: 12),
              icon: Image.asset(
                'assets/images/ic_share.png',
              ),
              onPressed: () {
                if (ObjectUtil.isEmpty(model) ||
                    ObjectUtil.isEmpty(model.posterUrl)) {
                  return;
                }
                Share.share(model.posterUrl ?? "", subject: Global.AppName);
              },
            ),
          ],
        ),
      ),
    );
  }

  //分享信息
  Widget shareCode() {
    double navHeight = ScreenUtil.getInstance().appBarHeight +
        ScreenUtil.getInstance().statusBarHeight;
    return Positioned(
      top: navHeight,
      left: 0,
      right: 0,
      bottom: 0,
      child: ListView(
        children: [
          SizedBox(
            height: Get.height / 3 - navHeight,
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              children: [
                Builder(builder: (context) {
                  if (status != LoadStatusType.success) {
                    return Container(
                      height: 358,
                      padding:
                          EdgeInsets.symmetric(horizontal: 20, vertical: 35),
                      decoration: BoxDecoration(
                        color: Global.kTheme.colorBackground1,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Center(child: StatusViews(status)),
                    );
                  }
                  return Container(
                    padding: EdgeInsets.symmetric(horizontal: 20, vertical: 35),
                    decoration: BoxDecoration(
                      color: Global.kTheme.colorBackground1,
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              "我的专属邀请码",
                              style: TextStyle(
                                  fontSize: 14,
                                  color: Global.kTheme.textPrimary2,
                                  fontFamily: Global.mySemiboldFont),
                            ),
                            SizedBox(
                              width: 18,
                            ),
                            InkWell(
                              onTap: () {
                                ClipboardData data =
                                    new ClipboardData(text: model.code ?? "");
                                Clipboard.setData(data);
                                EasyLoading.showToast("复制成功");
                              },
                              child: Row(
                                children: [
                                  Text(
                                    model.code ?? "",
                                    style: TextStyle(
                                        fontSize: 20,
                                        color: Color(0xFFF5A623),
                                        fontFamily: Global.mySemiboldFont),
                                  ),
                                  SizedBox(
                                    width: 4,
                                  ),
                                  LoadImage(
                                    "icon_copy_in",
                                    width: 16,
                                    height: 16,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 28,
                        ),
                        QrImage(
                          size: 129,
                          data: model.posterUrl ?? "",
                          padding: EdgeInsets.all(3),
                        ),
                        SizedBox(
                          height: 26,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              "我的邀请链接",
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Global.kTheme.textPrimary4,
                                  fontFamily: Global.mySemiboldFont),
                            ),
                            SizedBox(
                              width: 9,
                            ),
                            Expanded(
                              child: InkWell(
                                onTap: () {
                                  ClipboardData data = new ClipboardData(
                                      text: model.posterUrl ?? "");
                                  Clipboard.setData(data);
                                  EasyLoading.showToast("复制成功");
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(
                                      horizontal: 10, vertical: 3),
                                  decoration: BoxDecoration(
                                    color: Color(0xFFF7F6FA),
                                    borderRadius: BorderRadius.circular(100),
                                  ),
                                  child: Row(
                                    children: [
                                      Expanded(
                                        child: Text(
                                          model.posterUrl ?? "",
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(
                                              fontSize: 11,
                                              fontFamily: Global.mySemiboldFont,
                                              color:
                                                  Global.kTheme.textPrimary4),
                                        ),
                                      ),
                                      SizedBox(
                                        width: 4,
                                      ),
                                      LoadAssetImage(
                                        "icon_copy_link",
                                        width: 14,
                                        height: 14,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        // Container(
                        //   padding: EdgeInsets.symmetric(vertical: 8),
                        //   decoration: BoxDecoration(
                        //     color: Color(0xFFF5A623),
                        //     borderRadius: BorderRadius.circular(6),
                        //   ),
                        //   child: Center(
                        //       child: Text(
                        //         "保存图片",
                        //         style: TextStyle(
                        //             fontFamily: Global.mySemiboldFont,
                        //             color: Global.kTheme.textPrimary5,
                        //             fontSize: 14),
                        //       )),
                        // ),
                      ],
                    ),
                  );
                }),
                SizedBox(
                  height: 14,
                ),
                invitedRecord(),
                SizedBox(
                  height: 20,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  //邀请记录
  Widget invitedRecord() {
    return Container(
        padding: EdgeInsets.symmetric(
          vertical: 10,
        ),
        decoration: BoxDecoration(
          color: Global.kTheme.colorBackground1,
          borderRadius: BorderRadius.circular(20),
        ),
        child: Column(
          children: [
            Container(
              height: 42,
              decoration: BoxDecoration(
                  border: Border(
                bottom: Divider.createBorderSide(context,
                    color: Global.kTheme.colorLine, width: 1),
              )),
              child: Stack(
                children: [
                  Align(
                      alignment: Alignment.center,
                      child: Text(
                        "邀请记录",
                        style: TextStyle(
                            fontSize: 14,
                            fontFamily: Global.mySemiboldFont,
                            color: Global.kTheme.textPrimary2),
                      )),
                  InkWell(
                    onTap: () {
                      Get.to(MyInviteRecordPage());
                    },
                    child: Align(
                        alignment: Alignment.centerRight,
                        child: Padding(
                          padding: const EdgeInsets.only(right: 20),
                          child: Text(
                            "查看详情",
                            style: TextStyle(
                                fontSize: 12, color: Color(0xFF999999)),
                          ),
                        )),
                  )
                ],
              ),
            ),
            Container(
              height: 80,
              child: Padding(
                padding: const EdgeInsets.only(top: 15, left: 40, right: 40),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    invitedRecordItem(
                        UserController.to.member.inviteCount ?? "0", "我的直推"),
                    invitedRecordItem(
                        UserController.to.member.teamCount ?? "0", "我的团队"),
                  ],
                ),
              ),
            ),
          ],
        ));
  }

  Widget invitedRecordItem(String top, String bottom) {
    return Column(
      children: [
        Text(
          top,
          style: TextStyle(
              fontSize: 15,
              color: Global.kTheme.textPrimary4,
              fontFamily: Global.myDINFont),
        ),
        SizedBox(
          height: 2,
        ),
        Text(
          bottom,
          style: TextStyle(
            fontSize: 12,
            color: Color(0xFF8B8B93),
          ),
        ),
      ],
    );
  }
}
